package org.yray.graphql;

import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.schema.GraphQLSchema;
import graphql.schema.idl.RuntimeWiring;
import graphql.schema.idl.SchemaGenerator;
import graphql.schema.idl.SchemaParser;
import graphql.schema.idl.TypeDefinitionRegistry;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.util.List;
public class GraphQLSDLDemo {
    /**
     * 定义Schema
     * <p>
     * schema { #定义查询
     *   query: UserQuery
     * }
     *
     * @return
     */
    public static GraphQLSchema createGraphqlSchema(TypeDefinitionRegistry
                                                            typeRegistry, RuntimeWiring wiring) {
        SchemaGenerator schemaGenerator = new SchemaGenerator();
        return schemaGenerator.makeExecutableSchema(typeRegistry, wiring);
    }
    /**
     * 定义类型的注册器
     *
     * @param fileContent
     * @return
     */
    public static TypeDefinitionRegistry createTypeDefinitionRegistry(String
                                                                              fileContent){
        SchemaParser schemaParser = new SchemaParser();
        return schemaParser.parse(fileContent);
    }
    /**
     * 读取文件内容
     *
     * @param fileName
     * @return
     */
    public static String readFileToString(String fileName){
        try {
            return
                    IOUtils.toString(GraphQLSDLDemo.class.getClassLoader().getResourceAsStream(fileName
                    ), "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    /*public static RuntimeWiring createRuntimeWiring() {
        return RuntimeWiring.newRuntimeWiring()
                .type("UserQuery", typeWiring -> typeWiring
                        .dataFetcher("user", environment -> {
                            Long id = environment.getArgument("id");
                            return new User(id, "张三_"+id, 20 + id.intValue());
                        })
                ).build();
    }*/
    public static void main(String[] args) {
        String fileName = "usersdl.graphqls";
        TypeDefinitionRegistry registry =
                createTypeDefinitionRegistry(readFileToString(fileName));
        RuntimeWiring runtimeWiring = createRuntimeWiring();
        GraphQL graphQL = GraphQL.newGraphQL(createGraphqlSchema(registry,
                runtimeWiring)).build();
        // 注意：查询语句中的user是小写，要和user.graphqls文件中的属性名一致
        String query = "{user(id:1){id,name,age},Card{cardNumber}}";
        ExecutionResult executionResult = graphQL.execute(query);
        System.out.println("查询字符串：" + query);
        // 打印错误
        System.out.println("错误：" + executionResult.getErrors());
        // 打印数据
        System.out.println("结果：" +(Object) executionResult.getData());
    }

    public static RuntimeWiring createRuntimeWiring() {

        return RuntimeWiring.newRuntimeWiring()
                .type("UserQuery", typeWiring -> typeWiring
                        .dataFetcher("user", environment -> {
                            Long id = environment.getArgument("id");
                            return new UserSdl(id, "张三_"+id, 20 + id.intValue());
                        })
                ).type("Card",typeWiring -> typeWiring
                        .dataFetcher("card", environment -> {
                            Long id = environment.getArgument("id");
                            return new
                                    Card("number_"+id, id);
                        })).build();
    }

}
